/**
  *@file    drv_key.c
  *@brief   driver key interface
  *@create  zxf 2021-06-21
  *@version V1.0.0
  *@note    none
  */
#include "drv_key.h"
#include "board_pin.h"

/* Private define ---------------------------------------*/

/* Private macro ----------------------------------------*/

#define KEY1_GET()                     GPIO_Get_In(KEY01_PORT, KEY01_PIN)

/* Private typedef --------------------------------------*/

typedef struct
{
    u8  state;
    u8  value;
    u32 delay;
}keyModel_t;

/* Private variables ------------------------------------*/

static keyModel_t sKeyObj[KEY_NUM];

/* Private function prototypes --------------------------*/
/* Interface variables ----------------------------------*/

/* Interface functions ----------------------------------*/

void drvKey_Init(void)
{
    GPIO_InitType  GPIO_InitStructure;
    
    GPIO_InitStruct(&GPIO_InitStructure);
    
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
    GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
    GPIO_InitStructure.Pin = KEY01_PIN;
    GPIO_InitPeripheral(KEY01_PORT, &GPIO_InitStructure);

    memset(&sKeyObj, 0x00, sizeof(keyModel_t)*KEY_NUM);
}

void drvKey_Scan(u32 tp)
{
    if(KEY1_GET() == 0)
    {
        if((sKeyObj[0].state&0x03) != 0x03)
            sKeyObj[0].delay += tp;
        
        if(sKeyObj[0].delay >= 100)
        {
            if((sKeyObj[0].state&0x01) == 0)
            {
                sKeyObj[0].value = KEY_VAL_01;
                sKeyObj[0].state |= 0x01;
            }
        }
        if(sKeyObj[0].delay >= 2000)
        {
            if((sKeyObj[0].state&0x02) == 0)
            {
                sKeyObj[0].value = KEY_VAL_01L;
                sKeyObj[0].state |= 0x02;
            }
        }
        
    }
    else
    {
        sKeyObj[0].delay = 0;
        sKeyObj[0].state = 0;
        sKeyObj[0].value = 0;
    }
    
    /*if(KEY2_GET() == 0)
    {
        if((sKeyObj[1].state&0x03) != 0x03)
            sKeyObj[1].delay += tp;
        
        if(sKeyObj[1].delay >= 100)
        {
            if((sKeyObj[1].state&0x01) == 0)
            {
                sKeyObj[1].value = KEY_VAL_02;
                sKeyObj[1].state |= 0x01;
            }
        }
        
        if(sKeyObj[1].delay >= 2000)
        {
            if((sKeyObj[1].state&0x02) == 0)
            {
                sKeyObj[1].value = KEY_VAL_02L;
                sKeyObj[1].state |= 0x02;
            }
        }
    }
    else
    {
        sKeyObj[1].delay = 0;
        sKeyObj[1].state = 0;
        sKeyObj[1].value = 0;
    }*/
}

u32 drvKey_Read(u8 kId)
{
    u32 kval;
    
    if(kId >= KEY_NUM)
        return 0;
    
    kval = sKeyObj[kId].value;
    sKeyObj[kId].value = 0;
    
    return kval;
}

/* Private functions ------------------------------------*/



